var laypage = layui.laypage;
let page = {
    pagenum:1,
    pagesize:2,
    
}
//获取列表‘
list();

//layui分页初始化
function pagelist(data){
     //执行一个laypage实例
    laypage.render({
    elem: 'articlePage',
    count: data.total ,//数据总数，从服务端得到,
    layout:['prev', 'page', 'next','skip','count','limit'],
    limits:[2,4,6],
    limit:page.pagesize,
    curr:page.pagenum
,
    
    jump: function(obj, first){
      //obj包含了当前分页的所有参数，比如：
    //   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
    //   console.log(obj.limit); //得到每页显示的条数
      //首次不执行
      if(!first){
        if(obj.limit!=page.pagesize){
            page.pagenum = 1;
        }else{
            page.pagenum=obj.curr;
        }
        page.pagesize=obj.limit;
        list()
      }

   
    }
  });
 }
function list(){
    $.ajax({
        url:'/admin/users',
        data:page,
        success:function(res){
            let str='';
            if(res.status==0){
                res.data.forEach(item => {
                    str+=`
                     <tr>
                     <td>${item.id}</td>
                     <td>${item.username}</td>
                     <td>${item.nickname}</td>
                     <td>${item.email}</td>
                     <td>
                     
                        <button type="button" class="layui-btn compile" data-id='${item.id}'>
                        <a style="color:#fff" target='iframeArea' href='./edit.html?id=${item.id}'>编辑
                        </a></button>
                        <button type="button" class="layui-btn layui-btn-danger delete" data-id='${item.id}'>删除</button>
                        <button type="button" class="layui-btn layui-btn-normal bb" data-id='${item.id}'>重置密码</button>
                    </td></tr>
                    `;
                });
                pagelist(res);
            }
            $('tbody').html(str);
    
        }
    })
}
let str=`<form class="layui-form add-form" action="" style="margin: 30px; margin-left: 0px;" id="add_form">
<input type="hidden" name="id" class="ni">
<div class="layui-form-item">
  <label class="layui-form-label">新密码</label>
  <div class="layui-input-block">
    <input type="text" name="name" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input cc">
  </div>
</div>
<div class="layui-form-item">
  <label class="layui-form-label">确认密码</label>
  <div class="layui-input-block">
    <input type="text" name="alias" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
  </div>
</div>
<div class="layui-form-item">
  <div class="layui-input-block">
    <button class="layui-btn layui-btn-normal queren" lay-submit lay-filter="formDemo">确认修改</button>
    <button type="reset" class="layui-btn layui-btn-primary layui-border-green">重置</button>
  </div>
</div>
</form>`
// 重置密码
let layer =layui.layer
$('tbody').on('click','.bb',function(e){
let a=$(this).attr('data-id');
layer.open({
  title: '重置密码',
  content: str,
  area: ['500px', '300px'],
  btn:[]
});  


$('#add_form').on('submit',function(e){
  e.preventDefault();

  $.ajax({
   type: 'put',
        url: '/admin/users/' + a,
        data: {
          password: $('.cc').val()
        },
    success:function(res){
      layer.msg(res.message);
      
    }
  })
})
})

$("tbody").on("click",".delete",function(){
  let id = $(this).attr('data-id');
      $.ajax({
        url:'/admin/users/'+id,
        type:'DELETE',
        success:function(res){
            let str='';
            if(res.status==0){
              page = {
                pagenum:1,
                pagesize:2,
                
            }
               list();
            }
            $('tbody').html(str);

        }
     })
})